package com.example.mybank.config;

import com.example.mybank.entity.Account;
import com.example.mybank.entity.Users;
import com.example.mybank.service.AccountService;
import com.example.mybank.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

@Configuration
public class BankAccountConfig {

    @Autowired
    AccountService accountService;

    @Autowired
    UsersService usersService;

    @Bean(name="bankAccount")
    public Account getBankAccount(){
        Account bankAccount=null;
        bankAccount=accountService.getFirstBankAccount();
        if(bankAccount!=null){
            return bankAccount;
        }else{
            Users admin=usersService.getFirstAdmin();
            Date now=new Date();
            if(admin==null){
                admin = new Users(0L,"bank","bank","bank","bank",Boolean.FALSE,0,Boolean.TRUE,Boolean.TRUE,now,now);
                admin=usersService.createAdmin(admin);
            }
            bankAccount = new Account(0L,0L,"bank", admin.getId(),now,Boolean.TRUE);
            accountService.createBankAccount(bankAccount);
            return bankAccount;
        }

    }

}
